package org.feit.findword.view;

import org.feit.findword.R;
import org.feit.findword.game.Board;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class BoardView extends View {
	private static final String TAG = "BoardView";
	
	static final int PADDING = 10;

	private Board mBoard;
	private Paint mPaint;
	private long mHighlighted;
	private Bitmap mConsonant;
	private Bitmap mVocalLetter;
	private Bitmap mOver;
	private boolean mIsLoaded;

	public BoardView(Context context, Board board) {
		super(context);
		Log.d(TAG, "BoardView consturctor");

		mBoard = board;
		initView();

	}

	public BoardView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}

	public BoardView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();

	}

	private void initView() {
		mHighlighted = 0;

		if (mBoard.getWidth() == 4) {
			mVocalLetter = BitmapFactory.decodeResource(getResources(),
					R.drawable.c2_4);
			mConsonant = BitmapFactory.decodeResource(getResources(),
					R.drawable.c1_4);
			mOver = BitmapFactory.decodeResource(getResources(),
					R.drawable.c3_4);
		}
		if (mBoard.getWidth() == 5) {
			mVocalLetter = BitmapFactory.decodeResource(getResources(),
					R.drawable.c2_5);
			mConsonant = BitmapFactory.decodeResource(getResources(),
					R.drawable.c1_5);
			mOver = BitmapFactory.decodeResource(getResources(),
					R.drawable.c3_5);
		}
		if (mBoard.getWidth() == 6) {
			mVocalLetter = BitmapFactory.decodeResource(getResources(),
					R.drawable.c2_6);
			mConsonant = BitmapFactory.decodeResource(getResources(),
					R.drawable.c1_6);
			mOver = BitmapFactory.decodeResource(getResources(),
					R.drawable.c3_6);
		}
		mPaint = new Paint();
		mPaint.setTextAlign(Paint.Align.CENTER);
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(1);
		mIsLoaded = true;
	}

	public void setBoard(Board board) {
		mBoard = board;
	}

	public void setHighlighted(long highlighted) {
		mHighlighted = highlighted;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int width = getWidth();
		int height = getHeight();
		// Draw round rectangle
		mPaint.setStrokeWidth(2);
		mPaint.setARGB(255, 0xff, 0xcc, 0xaa);
		canvas.drawRoundRect(new RectF(PADDING - 2, PADDING - 2, width
				- PADDING + 2, height - PADDING + 2), 10, 10, mPaint);

		mPaint.setARGB(255, 0xff, 0xcc, 0xaa);
		if (mBoard == null || !mIsLoaded)
			return;
		
		float boxSize = 1.0f * (width - 2 * PADDING) / mBoard.getWidth();
		// Draw letter circles
		for (int i = 0; i < mBoard.getSize(); i++) {
			int x = i % mBoard.getWidth();
			int y = i / mBoard.getWidth();
			float left = PADDING + boxSize * x;
			float top = PADDING + boxSize * y;

			if (((1L << i) & mHighlighted) == 0) {
				if (mBoard.isVocal(i)) {
					canvas.drawBitmap(mVocalLetter, left + 1, top + 1, mPaint);
				} else {
					canvas.drawBitmap(mConsonant, left + 1, top + 1, mPaint);
				}
			} else {
				canvas.drawBitmap(mOver, left + 1, top + 1, mPaint);
			}
		}

		mPaint.setARGB(255, 0, 0, 0);
		mPaint.setTextSize(boxSize - 22);
		mPaint.setTextAlign(Paint.Align.CENTER);

		mPaint.setTypeface(Typeface.MONOSPACE);
		for (int x = 0; x < mBoard.getWidth(); x++) {
			for (int y = 0; y < mBoard.getWidth(); y++) {
				String txt = mBoard.elementAt(x, y);
				canvas.drawText(txt, PADDING + x * boxSize + boxSize / 2,
						PADDING - 15 + (y + 1) * boxSize, mPaint);
			}
		}

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int side = Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec
				.getSize(heightMeasureSpec));

		setMeasuredDimension(side, side);
	}

}
